package com.budwk.app.sqfw.component;

import cn.hutool.core.collection.CollectionUtil;
import com.budwk.app.common.config.AppException;
import com.budwk.app.sqfw.constants.CommonConstants;
import com.budwk.app.sqfw.dto.building.BuildQueryReqVo;
import com.budwk.app.sqfw.dto.building.BuildingInfoDTO;
import com.budwk.app.sqfw.models.BuildingInfo;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfw.services.BuildingInfoService;
import com.budwk.app.sqfwV2.dto.vo.BuildingUnitVo;
import com.budwk.app.sqfwV2.dto.vo.QueryCondititonVo;
import com.budwk.app.util.poiUtils.nutzBeanUtils.NutzIocUtils;
import com.budwk.starter.common.page.Pagination;
import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Cnd;
import org.nutz.dao.util.cri.SqlExpressionGroup;

import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.List;
import java.util.Optional;

public class BuildingInfoComonent {

    public static void checkSubmitParam(BuildingInfoDTO dto) {




    }

    public static void checkDeleteParam(BuildingInfo info) {
        if (StringUtils.isEmpty(info.getId())) {
            throw new AppException("删除数据id不能为空！");
        }
        if (StringUtils.isEmpty(info.getDeleteReason())) {
            throw new AppException("删除原因不能为空！");
        }
    }

    public static Pagination handleData(Pagination pagination, BuildQueryReqVo reqVo) {
        int totalCount = pagination.getTotalCount();
        if (totalCount == 0) {
            return pagination;
        }
        List<BuildingInfo> list = pagination.getList(BuildingInfo.class);
        // 查询最后一个月的数据量
        int total = queryLastMonthTotal(list, reqVo);
        pagination.setTotalCount(total);
        return pagination;
    }

    public static SqlExpressionGroup buildCommonSqlExpress(Cnd cnd, BuildQueryReqVo reqVo) {
        if (StringUtils.isEmpty(reqVo.getSort())) {
            // 默认按照降序处理
            reqVo.setSort("desc");
        }
        SqlExpressionGroup where = cnd.where();
        if (StringUtils.isNotEmpty(reqVo.getQueryValue())) {
            // 小区名称 楼栋名称 网格
            SqlExpressionGroup a1 = Cnd.exps(BuildingInfo::getResidentialArea, "like", "%" + reqVo.getQueryValue() + "%");
            SqlExpressionGroup a2 = Cnd.exps(BuildingInfo::getBuildingName, "like", "%" + reqVo.getQueryValue() + "%");
            SqlExpressionGroup a3 = Cnd.exps(BuildingInfo::getGrid, "like", "%" + reqVo.getQueryValue() + "%");
            where.and(a1.or(a2).or(a3));
        }
        List<QueryCondititonVo> queryCondititons = reqVo.getQueryCondititons();
        if (CollectionUtil.isNotEmpty(queryCondititons)) {
            for (QueryCondititonVo condititonVo : queryCondititons) {
                if (StringUtils.isNotEmpty(condititonVo.getKey()) && StringUtils.isNotEmpty(condititonVo.getValue())) {
                    if (condititonVo.getKey().equals("floorsCount")) {
                        if (condititonVo.getValue().equals("1-8层")) {
                            where.andBetween(condititonVo.getKey(), 1, 8);
                            continue;
                        }
                        if (condititonVo.getValue().equals("8-30层")) {
                            where.andBetween(condititonVo.getKey(), 8, 30);
                            continue;
                        }
                        if (condititonVo.getValue().equals("30层以上")) {
                            where.andGT(condititonVo.getKey(), 30);
                            continue;
                        }
                        continue;
                    }
                    where.andEquals(condititonVo.getKey(), condititonVo.getValue());
                }
            }
        }
        where.andEquals(BuildingInfo::isDeleted, false);
        return where;
    }

    private static int queryLastMonthTotal(List<BuildingInfo> list, BuildQueryReqVo reqVo) {
        BuildingInfo summaryNew = list.get(list.size() - 1);
        Cnd cnd = Cnd.NEW();
        // 组装公共查询条件
        SqlExpressionGroup express = buildCommonSqlExpress(cnd, reqVo);
        // 获取当月第一天和最后一天
        LocalDate date = summaryNew.getFillDate().toLocalDate();
        String lastDay = date.with(TemporalAdjusters.lastDayOfMonth()) + CommonConstants.LAST_TIME; // 当月最后一天
        String startDay = date.with(TemporalAdjusters.firstDayOfMonth()) + CommonConstants.LAST_TIME; // 当月第一天
        express.andBetween(Sqfw_formSummary_new::getFillDate, startDay, lastDay);
        BuildingInfoService buildingInfoService = NutzIocUtils.getBean(BuildingInfoService.class);
        return buildingInfoService.count(cnd);
    }

    public static void checkUpdateParam(BuildingInfoDTO dto, BuildingInfo b) {
        if (b == null) {
            throw new AppException("数据不存在！");
        }
        BuildingInfo reqBuild = dto.getBuildingInfo();
        if (StringUtils.isEmpty(reqBuild.getId())) {
            throw new AppException("参数id不能为空！");
        }
        // 单元数不能小于原数据单元数
        if (reqBuild.getUnitNumber() < b.getUnitNumber()) {
            throw new AppException("单元数不能小于当前单元数！");
        }
        // 楼层不能低于当前楼层
        if (reqBuild.getFloorsCount() < b.getFloorsCount()) {
            throw new AppException("楼层不能低于当前楼层！");
        }
        // 单元中层数不能低于当前单元的层数
        List<BuildingUnitVo> buildUnits = b.getBuildUnits();
        if (CollectionUtil.isEmpty(buildUnits)) {
            return;
        }
        if (CollectionUtil.isEmpty(reqBuild.getBuildUnits())) {
            return;
        }
        for (BuildingUnitVo buildUnit : reqBuild.getBuildUnits()) {
            // 从原有单元中取出单元信息
            Optional<BuildingUnitVo> any = buildUnits.stream().filter(e -> e.getUnitName().equals(buildUnit.getUnitName())).findAny();
            if (any.isPresent()) {
                // 原来存储的单元信息
                BuildingUnitVo buildingUnitVo = any.get();
                // 比较当前提交的单元层数是否小于原来的单元层数
                if (buildUnit.getFloorsCount() < buildingUnitVo.getFloorsCount()) {
                    throw new AppException(buildUnit.getUnitName() + "楼层数不能低于当前楼层！");
                }
            }
        }
    }
}
